package cn.iocoder.yudao.module.hs.controller.admin.qchistory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.hs.controller.admin.qchistory.vo.*;
import cn.iocoder.yudao.module.hs.dal.dataobject.qchistory.QcHistoryDO;
import cn.iocoder.yudao.module.hs.service.qchistory.QcHistoryService;

@Tag(name = "管理后台 - 物料质检历史记录")
@RestController
@RequestMapping("/hs/qc-history")
@Validated
public class QcHistoryController {

    @Resource
    private QcHistoryService qcHistoryService;

    @PostMapping("/create")
    @Operation(summary = "创建物料质检历史记录")
    @PreAuthorize("@ss.hasPermission('hs:qc-history:create')")
    public CommonResult<Long> createQcHistory(@Valid @RequestBody QcHistorySaveReqVO createReqVO) {
        return success(qcHistoryService.createQcHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料质检历史记录")
    @PreAuthorize("@ss.hasPermission('hs:qc-history:update')")
    public CommonResult<Boolean> updateQcHistory(@Valid @RequestBody QcHistorySaveReqVO updateReqVO) {
        qcHistoryService.updateQcHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料质检历史记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:qc-history:delete')")
    public CommonResult<Boolean> deleteQcHistory(@RequestParam("id") Long id) {
        qcHistoryService.deleteQcHistory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料质检历史记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:qc-history:query')")
    public CommonResult<QcHistoryRespVO> getQcHistory(@RequestParam("id") Long id) {
        QcHistoryDO qcHistory = qcHistoryService.getQcHistory(id);
        return success(BeanUtils.toBean(qcHistory, QcHistoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料质检历史记录分页")
    @PreAuthorize("@ss.hasPermission('hs:qc-history:query')")
    public CommonResult<PageResult<QcHistoryRespVO>> getQcHistoryPage(@Valid QcHistoryPageReqVO pageReqVO) {
        return success(qcHistoryService.getQcHistoryPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料质检历史记录 Excel")
    @PreAuthorize("@ss.hasPermission('hs:qc-history:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQcHistoryExcel(@Valid QcHistoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "物料质检历史记录.xls", "数据", QcHistoryRespVO.class,
                qcHistoryService.getQcHistoryPage(pageReqVO).getList());
    }

}